package cn.huihuo.weibo.controller;

import cn.huihuo.weibo.bean.ProdCommentBean;
import cn.huihuo.weibo.bean.dynamic.Dynamic;
import cn.huihuo.weibo.bean.participation.Participation;
import cn.huihuo.weibo.bean.product.ProductUGCBean;
import cn.huihuo.weibo.bean.product.UGCBean;
import cn.huihuo.weibo.module.*;
import cn.huihuo.weibo.service.ParticipationService;
import cn.huihuo.weibo.service.ProductCommentService;
import cn.jfinalbbs.common.BaseController;
import cn.jfinalbbs.common.Constants;
import cn.jfinalbbs.user.User;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品评论控制器
 * @author Dan
 * Created by zhengjunbo on 2015/9/25.
 */
public class ProductCommentController extends BaseController {


    public void index(){
        String prodCommentId = getPara();
        ProdComment prodComment = ProdComment.prodComment.findWeiBoById(prodCommentId);//获取整个商品

        setAttr("prodComment",prodComment);
        //获取商品图片
        List<ProductPic> prodPics = ProductPic.productPic.findByProductId(prodComment.getInt("prod_id"));

        setAttr("prodPics",prodPics);
        User user = getSessionAttr(Constants.USER_SESSION);
        setAttr("participationSize", user==null?"": ParticipationService.service.findTotalParticipation(user.getStr("id")));
        //获取商品的渠道信息

        List<BuyChannel> buyChannels = BuyChannel.buyChannel.getBuyChannelByProdId(prodComment.getInt("prod_id"));
        for(BuyChannel buyChannel:buyChannels){
            if("bingfan".equals(buyChannel.get("channel_type"))){
                setAttr("bingfan",buyChannel);
            }
            if("hai360".equals(buyChannel.get("channel_type"))){
                setAttr("hai360",buyChannel);
            }
        }

        //跳转到页面
        render("huihuo/product.html");
    }

    public void findUGC(){
        String count = getPara("count");
        String pageStep = getPara("pageStep");
        String productId = getPara("productId");
        String ugcType = getPara("ugcType");

        User user = getSessionAttr(Constants.USER_SESSION);
        String userId =null;
        if(user !=null){
            userId = user.getStr("id");
        }
        ProductUGCBean productUGCBean = ProductCommentService.service.findUGCByProdId(productId, ugcType, pageStep, count,userId);
        success(productUGCBean);
    }

    public void findUGCById(){
        String ugcId = getPara("ugcId");
        User user = getSessionAttr(Constants.USER_SESSION);
        String userId =null;
        if(user !=null){
            userId = user.getStr("id");
        }
        ProductUGCBean productUGCBean = ProductCommentService.service.findUGCByUGCId(ugcId,userId);
        success(productUGCBean);
    }

    public void addUGC(){
        Integer productId = getParaToInt("productId");
        String ugcType = getPara("ugcType");
        String url = getPara("url");
        String prodComment = getPara("prodComment");

        User user = getSessionAttr(Constants.USER_SESSION);
        if(user==null){
            error("请登录后添加！");
            return;
        }
        ParticipationService.service.tryPutParticipation(user.getStr("id"),productId);

        ProdComment comment = new ProdComment();


        //检查 ugc类型和ugc的url，如果url不为空则去检查如果相同，则抛出异常。
        if(url!=null){
            ProdComment sameUrl = ProdComment.prodComment.findSameUrl(productId, ugcType, url);
            if(sameUrl!=null){
                error("链接已存在！");
                return;
            }
        }

        boolean save ;
        try {
            save = comment.set("product_comment", prodComment)
                    .set("prod_id", productId)
                    .set("user_id", user.getStr("id"))
                    .set("ugc_type", ugcType)
                    .set("url", url)
                    .save();
        } catch (Exception e) {
            e.printStackTrace();
            error("保存失败！");
            return;
        }
        ParticipationService.service.updateParticipation(productId);



        if(save){
            List<UGCBean> ugcBeans = new ArrayList<>();
            UGCBean ugcBean = new UGCBean();
            ugcBean.setBadChannelNum("0");
            ugcBean.setGoodChannelNum("0");
            ugcBean.setNormalChannelNum("0");
            ugcBean.setChooseChannel("0");
            ugcBean.setTime("刚刚");
            ugcBean.setProdComment(prodComment);
            ugcBean.setUgcId(String.valueOf(comment.getInt("id")));
            ugcBean.setUrl(url);
            ugcBean.setUrlDescription("");
            ugcBean.setUgcType(ugcType);
            ugcBean.setCommentNum("0");
            ugcBean.setTypeDescription("");
            ugcBean.setUserId(user.getStr("id"));
            ugcBean.setUserAvatar(user.getStr("avatar"));
            ugcBean.setUserName(user.getStr("nickname"));
            ugcBean.setUserUrl("");
            ugcBeans.add(ugcBean);
            success(ugcBeans);
        } else {
            error("保存失败！");
        }

    }

    @Before(Tx.class)
    public void delete(){
        String cid = getPara("cid");
        if(cid==null){
            error("未看见删除对象");
            return;
        }
        boolean b = ProdComment.prodComment.deleteById(cid);
        if(!b){
            error("删除名称失败！");
            return;
        }

        List<Comment> comments = Comment.comment.findCommentByUGCId(cid);
        for(Comment comment:comments){
            Integer id = comment.getInt("id");
            Message.dao.deleteMessage(id);
        }
        Comment.comment.deleteByProdCommId(cid);

        success();
    }

    public void findDynamic(){
        Integer pageStep = getParaToInt("pageStep");
        Integer count = getParaToInt("count");
        String userId = getPara("userId");
        Dynamic dynamic;
        if(userId!=null){
             dynamic = ProductCommentService.service.assembleDynamic(pageStep, count,userId);
        } else {
             dynamic = ProductCommentService.service.assembleDynamic(pageStep, count);
        }

        success(dynamic);
    }
}
